//
//  GridHomeViewController.swift
//  grid-ios-app
//
//  Created by yanlu on 2024/3/17.
//

import UIKit
import SnapKit

struct GridHomeModel {
    let name: String
    let picName: String
}

class GridHomeViewController: UIViewController {
    
    //    人员管理、事件上报、蓝牙、NFC、扫码、工作轨迹、考勤打卡和工作统计
    private let dataItem: Array<GridHomeModel> = [
        GridHomeModel(name: "人员管理", picName: "home_1"),
        GridHomeModel(name: "事件上报", picName: "home_1"),
        GridHomeModel(name: "蓝牙", picName: "home_1"),
        GridHomeModel(name: "NFC", picName: "home_1"),
        GridHomeModel(name: "扫码", picName: "home_1"),
        GridHomeModel(name: "工作轨迹", picName: "home_1"),
        GridHomeModel(name: "考勤打卡", picName: "home_1"),
        GridHomeModel(name: "工作统计", picName: "home_1"),
        GridHomeModel(name: "文件", picName: "home_1"),
        GridHomeModel(name: "树形结构", picName: "home_1")

       
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        view.backgroundColor = Theme.Color.Background.default
        navigationItem.title = "首页"
        collectionView.snp.makeConstraints { make in
            make.top.bottom.left.right.equalToSuperview()
        }
    }
    
    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: (UIScreen.main.bounds.width-20)/4.0, height: (UIScreen.main.bounds.width-20)/3.0)
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.sectionInset = UIEdgeInsets(top: 10, left: 10, bottom: 0, right: 10)
        let view = UICollectionView(frame: .zero, collectionViewLayout: layout)
        view.backgroundColor = .white
        //1.设置头部视图size
        layout.headerReferenceSize = CGSize(width: UIScreen.main.bounds.width, height: 100)
        //2.注册头部视图
        view.register(GridHomeCollectionHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "GridHomeCollectionHeader")
        
        //注册单元格
        view.register(GridHomeCollectionCell.self, forCellWithReuseIdentifier: "GridHomeCollectionCell")
        
        view.dataSource = self
        view.delegate = self
        view.showsVerticalScrollIndicator = false
        
        self.view.addSubview(view)
        return view
    }()
    
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destination.
     // Pass the selected object to the new view controller.
     }
     */
    
}

extension GridHomeViewController: UICollectionViewDelegate, UICollectionViewDataSource {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataItem.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GridHomeCollectionCell", for: indexPath) as! GridHomeCollectionCell
        if indexPath.row < dataItem.count {
            cell.updateHomeModel(model: dataItem[indexPath.row])
        }
        return cell
    }
    
    //3.添加header
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        if kind == UICollectionView.elementKindSectionHeader{
            let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "GridHomeCollectionHeader", for: indexPath) as! GridHomeCollectionHeader
            return header
        }
        return UICollectionReusableView()
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.row < self.dataItem.count {
            let model: GridHomeModel = self.dataItem[indexPath.row]
            switch model.name {
            case "人员管理":
                gridPersonnelController()
                break
            case "扫码":
                gridQRCodeController()
                break
            case "蓝牙":
                gridBluetoothController()
                break
            case "NFC":
                gridNFCController()
                break
            case "文件":
                gridDocumentViewController()
                break
            case "树形结构":
                gridTreeNodeViewController()
                break
            default:
                print(model.name)
            }
        }
    }
    
    // MARK: --扫码
    private func gridQRCodeController() {
        let vc = GridQRScanViewController()
        vc.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    // MARK: --蓝牙
    private func gridBluetoothController() {
        let vc = GridBluetoothViewController()
        vc.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    // MARK: --NFC
    private func gridNFCController() {
        let vc = GridNFCViewController()
        vc.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vc, animated: true)
    }
    // MARK: --人员管理
    private func gridPersonnelController() {
        let vc = GridPersonnelManagementViewController()
        vc.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    // MARK: --文件
    private func gridDocumentViewController() {
        let vc = GridDocumentViewController()
        vc.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    // MARK: --文件
    private func gridTreeNodeViewController() {
        let vc = GridTreeNodeViewController()
        vc.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
    
}
